/*
 * main.cc
 *
 *  Created on: 2013. 3. 15.
 *      Author: jaty
 */

#include <iostream>
#include <memory>
#include <sstream>
#include <pthread.h>
#include "testThread.h"

static pthread_cond_t   exit_cond;
static pthread_mutex_t  exit_mutex;


static void sighandler(
    int sig
) {
        cout << "Received signal [" << sig << "]" << endl;

    pthread_mutex_lock(&exit_mutex);
    pthread_cond_signal(&exit_cond);
    pthread_mutex_unlock(&exit_mutex);
}

static void setup_signal(
    void
) {
    struct sigaction sig_action;
    sig_action.sa_handler = sighandler;
    sigemptyset(&sig_action.sa_mask);
    sig_action.sa_flags = 0;
    sigaction(SIGTERM, &sig_action, NULL);
    sigaction(SIGKILL, &sig_action, NULL);
    sigaction(SIGINT, &sig_action, NULL);
}

int main(int argc, char* argv[])
{
	setup_signal();
	INIT_COND_MUTEX(exit_cond, exit_mutex);

	prmMQlib::CPrmMQlib * prmmqlib = NULL;
	prmmqlib = new prmMQlib::CPrmMQlib;

	string brokeruri="failover://(tcp://127.0.0.1:61616)";
	string appid="testMqProducer";
	string queueName="testMqQueueInstrument";

	prmmqlib->initPrmMQlib(brokeruri,appid);
	int retProd = prmmqlib->addProducer(queueName,true,false);

	cout << "addProducer Result=" << retProd << endl;

	testThread test(prmmqlib, brokeruri, queueName);

	COND_WAIT(exit_cond, exit_mutex);
	cout << "main condition signal released, stopping test thread..." << endl;
	test.stopRunning();
	delete prmmqlib;
}
